package com.qf.controller;

import com.qf.mapper.ItemMapper;
import com.qf.model.Item;
import com.qf.model.Subitem;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.ItemService;
import com.qf.service.SubitemService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

//    @RequestMapping("/findAll")
//    public List<Branch> findAll(){
//        List<Branch> all = branchService.getAll();
//        return all;
//    }


    /**
     * subitem列表分页查询+模糊查询
     *
     * @param queryPageBean
     * @return PageResult
     */
    @RequestMapping("/all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = itemService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新建item
     *
     * @param
     * @return Result
     */
    //前端传json
    @RequestMapping("/add")
    public Result add(@RequestBody Item item) {
        Result result = new Result();
        int i = itemService.add(item);
        if (i > 0) {
            result.setFlag(true);
            result.setMessage("添加成功");
            return result;
        }
        result.setFlag(false);
        result.setMessage("添加失败");
        return result;
    }

    /**
     * 编辑item回显数据
     *
     * @param iid
     * @return Result
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("iid") String iid) {
        Result result = new Result();
        Item item = itemService.findById(iid);

        if (ObjectUtils.isNotEmpty(item)) {
            result.setData(item);
            result.setFlag(true);
            result.setMessage("success");
            return result;
        } else {
            result.setFlag(false);
            result.setMessage("fail");
            return result;
        }
    }

    /**
     * 修改item
     * @param item
     * @return result
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Item item){
        Result result = new Result();
        int i = itemService.edit(item);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }

    /**
     * 删除subitem
     * @param iid
     * @return result
     */
    @RequestMapping("/delete")
    public Result delete(@RequestParam("iid") String iid){
        Result result = new Result();
        int i = itemService.delete(iid);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }
   }
